{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a5f1227e",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-11-8\n",
    "* 课程:python\n",
    "* week08(第十周)\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31d87779",
   "metadata": {},
   "source": [
    "# 1. 本周内容\n",
    "## 1. 列表的数据处理（列表的方法）\n",
    "> 1. 查询数据\n",
    ">> 1. index()按照索引值查询数据，并取出值\n",
    ">> 2. slice 切片方法取值\n",
    ">> 3. in 方法查询元素是否存在\n",
    "> 2. 增加数据\n",
    ">> 1. append() 增加在末尾\n",
    ">> 2. insert() 指定位置增加\n",
    "> 3. 删除数据\n",
    ">> 1. pop()指定数据删除\n",
    ">> 2. remove()指定结果(值)删除\n",
    ">> 3. clear()通通删除\n",
    "> 4. 修改数据\n",
    ">> 1. 找到index进行修改\n",
    "> 5. 其他方法\n",
    ">> 1. 排序：sort（）reverse（）\n",
    ">> 2. 复制：copy（）\n",
    "## 2. 元组\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "983d6aba",
   "metadata": {},
   "source": [
    "# 1.1 列表的删除\n",
    "\n",
    "> 1. pop()\n",
    ">> 1. 默认删除最后一位数据\n",
    ">> 2. 指定位置删除\n",
    ">> 3. 注意事项：避免index error的出现，先检查列表的长度\n",
    "> 2.remove()\n",
    ">> 1.指定结果（值）删除\n",
    ">> 2. 注意事项：避免valueerr的出现，先检查元素是否存在\n",
    "> 3.clear()\n",
    ">> 1. 直接清空整个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "df45e626",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小米', '小河', '小张', '小庞', '小时']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小米\",\"小河\",\"小张\",\"小庞\",\"小时\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cd844c69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小时'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8bc361e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小时'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a4d4b59a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小米', '小河', '小张', '小庞']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "80d5f3d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小河'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "93efa121",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efff4726",
   "metadata": {},
   "source": [
    "## 1.2. remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "956c8a4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小米', '小河', '小张', '小庞', '小时']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小米\",\"小河\",\"小张\",\"小庞\",\"小时\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3afdccfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小河', '小张', '小庞', '小时']\n"
     ]
    }
   ],
   "source": [
    "name_list.remove(\"小米\")\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0c21e632",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先查询是否存在小红\n",
    "\"小红\"in name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44c6dd79",
   "metadata": {},
   "source": [
    "## 1.3. clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b39fcf4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小米', '小河', '小张', '小庞', '小时']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小米\",\"小河\",\"小张\",\"小庞\",\"小时\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "82906995",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "bc055277",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71eee754",
   "metadata": {},
   "source": [
    "## 1.4. 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "906969a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [25,86,47,58,55,25,54,52,14,25]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1045da53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[14, 25, 25, 25, 47, 52, 54, 55, 58, 86]\n"
     ]
    }
   ],
   "source": [
    "age_list.sort()\n",
    "print(age_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7d391eb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[86, 58, 55, 54, 52, 47, 25, 25, 25, 14]\n"
     ]
    }
   ],
   "source": [
    "age_list.reverse()\n",
    "print(age_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59a9f952",
   "metadata": {},
   "source": [
    "## 1.5. 复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "b1ed4064",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小米', '小河', '小张', '小庞', '小时']\n",
      "2910025081088\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小米\",\"小河\",\"小张\",\"小庞\",\"小时\"]\n",
    "print(name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1d110b50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2910025081088\n"
     ]
    }
   ],
   "source": [
    "存档_name_list = name_list\n",
    "存档_name_list\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0b0d0d19",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"小白\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "987ea81f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小米', '小河', '小张', '小庞', '小时', '小白']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a6a63f1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小米', '小河', '小张', '小庞', '小时', '小白']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "存档_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9080f2d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "copy_name_list = name_list.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0fa40be3",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"小蕊\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "aa7bf6fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小米', '小河', '小张', '小庞', '小时']\n",
      "2910025081088\n"
     ]
    }
   ],
   "source": [
    "print(name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "53155b19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小米', '小河', '小张', '小庞', '小时', '小白', '小蕊', '小蕊']\n",
      "2910025081088\n"
     ]
    }
   ],
   "source": [
    "print(copy_name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd4e1833",
   "metadata": {},
   "source": [
    "## 2.元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "1c056ba8",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = 12345,54321,'hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a3c4fadf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(t)\n",
    "print(type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "24282fcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = (12345,54321,'hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "efcb9610",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "8cfe70fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "bf0960a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.index(54321)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "fae0d3e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.count(12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "e41dba1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2023, tm_mon=11, tm_mday=8, tm_hour=11, tm_min=38, tm_sec=2, tm_wday=2, tm_yday=312, tm_isdst=0)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7e30577",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
